package com.jiaquyun.jcps.popup

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Typeface
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.ui.main.ConfigActivity
import kotlinx.android.synthetic.main.popup_user_agree.view.*
import razerdp.basepopup.BasePopupWindow


class WebViewPopup(
    context: Context,
    val onConfirm: () -> Unit,
    val onCancel: () -> Unit
) : BasePopupWindow(context) {

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_user_agree)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.tvPopupConfirm.setOnClickListener {
            this.onConfirm()
        }
        contentView.tvPopupCancel.setOnClickListener {
            this.onCancel()
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    fun setWindows(activity: Activity) {
//        contentView.wvPopupContent.let {
//            it.webViewClient = WebViewClient()
//            it.settings.domStorageEnabled = true
//            it.settings.databaseEnabled = true
//            it.settings.javaScriptEnabled = true
//            it.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
//            it.loadUrl(url)
//        }
        val ss = SpannableString("你可阅读《服务协议》和《隐私协议》了解详细信息。如你同意，请点击同意开始接受我们的服务。")
        ss.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                ConfigActivity.openUrl(
                    activity = activity,
                    title = "用户协议",
                    url = "https://mall.jiaquyun.cn/rider/common/get_article_details?id=10"
                )
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ColorUtils.getColor(R.color.colorMain)
                ds.isUnderlineText = false
            }
        }, 4, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        ss.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                ConfigActivity.openUrl(
                    activity = activity,
                    title = "隐私协议",
                    url = "https://mall.jiaquyun.cn/rider/common/get_article_details?id=14"
                )
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = ColorUtils.getColor(R.color.colorMain)
                ds.isUnderlineText = false
            }
        }, 12, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        contentView.tvAgreeContent.text = ss
        contentView.tvAgreeContent.movementMethod = LinkMovementMethod.getInstance();
    }

    fun setGoneConfirm(isGone: Boolean): WebViewPopup {
        contentView.tvPopupConfirm.visibility =
            if (isGone) View.GONE else View.VISIBLE
        return this
    }

    fun setGoneCancel(isGone: Boolean): WebViewPopup {
        contentView.tvPopupCancel.visibility =
            if (isGone) View.GONE else View.VISIBLE
        contentView.tvPopupLine.visibility =
            if (isGone) View.GONE else View.VISIBLE
        return this
    }

}